<?php
class AppCourseAction extends Action { 
	/**
	 * 课程列表
	 * 入参,排序字段,app首页类型1234567
	 * get
 	**/
    public function courseList(){
        $course = M('Course');
        $course_image = M('Course_image');
        if(!empty($_GET['search'])){
	        $where['s_course.course_name'] = array('like',"%{$_GET['search']}%");
        }
        $user_long=$_GET['longt'];//经度$_GET['longt']
 		$user_lat=$_GET['lat'];//纬度$_GET['lat']
 		if(!empty($user_lat) && !empty($user_long)){
 			$distance_id=$this->updateUserDistance($user_long,$user_lat);
 		}
 		$renqi=$_GET['renqi'];
 		if(!empty($distance_id)){
 			if(empty($renqi)){
 				$order = 'temp_distance asc,create_time desc';
 			}else {
 				$order = 'comment_count desc,create_time desc';
 			}
	        if(empty($_GET['id'])){
	        	$courseList = $course ->field('DISTINCT s_course.*,ifnull(uc.collection_count,0) as collection_count,ifnull(ccomment.comment_count,0) as comment_count,c.authentication,ci.image,ud.temp_distance as temp_distance,c.long as lllll,c.lat as ttttt') 
	        	->join("left join (select count(id) as collection_count,course_id from s_user_collection group by course_id) as uc on uc.course_id= s_course.id 
	        	left join (select count(id) as comment_count,course_id from s_course_comment group by course_id) as ccomment on ccomment.course_id= s_course.id 
	        	left join s_center c on c.id = s_course.center_id 
	        	left JOIN s_course_image ci on s_course.id = ci.course_id and ci.type='1'
	        	left join s_user_distance ud on ud.center_id=c.id and ud.distance_id=$distance_id")->where($where)-> order($order) -> select();
	        }else{
		        $where['s_course.type'] = $_GET['id'];//app首页,体育运动1,亲子早教2,科学技术3,美术绘画4,乐器演奏5,声乐舞蹈6,国学书法7
		        $courseList = $course ->field('DISTINCT s_course.*,ifnull(uc.collection_count,0) as collection_count,ifnull(ccomment.comment_count,0) as comment_count,c.authentication,ci.image,ud.temp_distance as temp_distance') 
		        ->join("left join (select count(id) as collection_count,course_id from s_user_collection group by course_id) as uc on uc.course_id= s_course.id 
	        	left join (select count(id) as comment_count,course_id from s_course_comment group by course_id) as ccomment on ccomment.course_id= s_course.id 
	        	left join s_center c on c.id = s_course.center_id 
	        	left JOIN s_course_image ci on s_course.id = ci.course_id and ci.type='1'
	        	left join s_user_distance ud on ud.center_id=c.id and ud.distance_id=$distance_id")->where($where)-> order('temp_distance asc,create_time desc') -> select();
	        }
 		}else{
 			if(empty($_GET['id'])){
	        	$courseList = $course ->field('DISTINCT s_course.*,ifnull(uc.collection_count,0) as collection_count,ifnull(ccomment.comment_count,0) as comment_count,c.authentication,ci.image') 
	        	->join("left join (select count(id) as collection_count,course_id from s_user_collection group by course_id) as uc on uc.course_id= s_course.id 
	        	left join (select count(id) as comment_count,course_id from s_course_comment group by course_id) as ccomment on ccomment.course_id= s_course.id 
	        	left join s_center c on c.id = s_course.center_id 
	        	left JOIN s_course_image ci on s_course.id = ci.course_id and ci.type='1'")->where($where)-> order('create_time desc') -> select();
	        }else{
		        $where['s_course.type'] = $_GET['id'];//app首页,体育运动1,亲子早教2,科学技术3,美术绘画4,乐器演奏5,声乐舞蹈6,国学书法7
		        $courseList = $course ->field('DISTINCT s_course.*,ifnull(uc.collection_count,0) as collection_count,ifnull(ccomment.comment_count,0) as comment_count,c.authentication,ci.image') 
		        ->join("left join (select count(id) as collection_count,course_id from s_user_collection group by course_id) as uc on uc.course_id= s_course.id 
	        	left join (select count(id) as comment_count,course_id from s_course_comment group by course_id) as ccomment on ccomment.course_id= s_course.id 
	        	left join s_center c on c.id = s_course.center_id 
	        	left JOIN s_course_image ci on s_course.id = ci.course_id and ci.type='1'")->where($where)-> order('create_time desc') -> select();
	        }
 		}
        
        $user_distance=M('user_distance');
		$options['distance_id']=$distance_id;
		$user_distance->where($options)->delete();
        
//        echo var_dump($courseList);
//        $this->display();
		$result['courseList']=$courseList;
		$result['sear']=$_GET['search'];
	    $this->ajaxReturn($result,'JSON');
    }
    /**
     * 课程详细
     * 入参,课程id
     * get
     */
    public function courseDitail(){
    	$user_click = M('Course'); 
    	$where['id'] = $_GET['id'];//$_GET['id']
    	$count=$user_click->where($where)->getField('number');
    	//查看列表查看次数加一
    	$counts = (int)$count + 1;
    	$data['number'] = $counts;
    	$user_click->where($where)->data($data)->save();
    	//课程
    	$course = M('Course');
    	$courseList = $course->where($where)->find();
    	//二级课程
    	$two_course = M('Two_course');
    	$course_id['course_id'] = $_GET['id'];
    	$twoCourseList = $two_course->where($course_id)->select();
    	//课程图片
    	$course_image = M('Course_image');
    	$courseImageList = $course_image->where($course_id)->select();
    	//评论
    	$course_comment = M('Course_comment');
    	$courseCommentList = $course_comment->field('s_course_comment.*,u.username')->join('left join s_user u on u.id=s_course_comment.user_id')->where($course_id)->select();
    	//机构,学校
    	$center = M('Center');
    	$course_id_c['c.id'] = $_GET['id'];
    	$centerList = $center->field('s_center.*')->join('right join s_course c on c.center_id=s_center.id')-> where($course_id_c)->find();
    	
    	$result['course'] = $courseList;//对象课程
    	$result['twoCourseList'] = $twoCourseList;//列表二级课程
    	$result['courseImageList'] = $courseImageList;//列表课程图片
    	$result['courseCommentList'] = $courseCommentList;//列表评论
    	$result['centerList'] = $centerList;//对象机构,学校
    	$this->ajaxReturn($result,'JSON');
    }
     /**
     * 课程评论列表
     * 入参,课程id
     * get
     */
    public function courseCommentList(){
    	$where['course_id'] = $_GET['id'];//$_GET['id']
    	$course_comment = M('Course_comment');
    	$courseCommentList=$course_comment->where($where)->select();
    	$this->ajaxReturn($courseCommentList,'JSON');
    }
    /**
     * 评论课程
     * 入参,课程id,用户id(session获取),评论内容message
     * post
     */
    public function commentCourse(){
    	$data['course_id'] = $_POST['id'];//$_GET['id']
    	$data['user_id'] = $_SESSION['user']['id'];//$_GET['id']
    	$data['create_time'] = date('Y-m-d H:i',time());//$_GET['id']
    	$data['message'] = $_POST['id'];//$_GET['id']
    	$course_comment = M('Course_comment');
    	$course_comment->create();
    	$course_comment=$course_comment->data($data)->add();
    	$this->ajaxReturn($course_comment,'JSON');
    }
    /**
     * 申请体验表
     * 入参,学生姓名student_name,学生性别student_sex,学生生日date,家长姓名parent_name,家长性别parent_sex,家长电话phone
     * post
     */
    public function courseTypesq(){
    	if(empty($_SESSION['user'])){
    		$result['code']='0';
    		$this->ajaxReturn($result,'JSON');
    	}else{
	    	$course_type = M('Course_type');
	    	$courseType = $course_type ->order('id asc')-> select();
	    	$result['code']='1';
	    	$result['courseType'] = $courseType;
	    	$this->ajaxReturn($result,'JSON');
    	}
    }
    /**
     * 分类表
     */
    public function courseType(){
    	$course_type = M('Course_type');
    	$courseType = $course_type ->order('id asc')-> select();
    	$result['code']='1';
    	$result['courseType'] = $courseType;
    	$this->ajaxReturn($result,'JSON');
    }
    /**
     * 全部评论
     */
    public function commentAll(){
    	$course_comment = M('Course_comment');
    	$where['course_id'] = $_GET['id'];
    	$courseComment = $course_comment->field('s_course_comment.*,u.username')->join('left join s_user u on u.id = s_course_comment.user_id') ->where($where)->order('create_time desc')-> select();
    	$result['code']='1';
    	$result['courseComment'] = $courseComment;
    	$this->ajaxReturn($result,'JSON');
    }
    /**
     * 我的收藏
     */
    public function collectionList(){
    	$user_collection = M('User_collection');
    	$where['s_user_collection.user_id'] = $_GET['id'];
    	$userCollection = $user_collection->field("s_user_collection.*,c.course_name,ci.image,sc.name as center_name,c.type as course_type") 
    	->join("left join s_course c on c.id = s_user_collection.course_id 
    		left join s_center sc on c.center_id = sc.id 
        	left JOIN s_course_image ci on c.id = ci.course_id and ci.type='1'")
    	->where($where) ->order('s_user_collection.create_time desc')-> select();
    	$result['code']='1';
    	$result['userCollection'] = $userCollection;
    	$this->ajaxReturn($result,'JSON');
    }
    /**
     *收藏 
     */
    public function addCollection(){
    	$user_collection = M('User_collection');
    	$user_collection->create();
		$curr_time = date('Y-m-d H:i',time());
		$user_collection->create_time=$curr_time;
		$user_collection->user_id= $_GET['user_id'];
		$user_collection->course_id=$_GET['course_id'];
		$id = $user_collection->add();
		if(empty($id)){
			$result['code']='1';
			$result['msg']='收藏失败';
			$this->ajaxReturn($result,'JSON');
		}else{
			$result['code']='2';
			$result['msg']='收藏成功';
			$this->ajaxReturn($result,'JSON');
		}
    }
    
    /**
     * 取消收藏
     */
	public function delCollection(){
    	$game = M("User_collection");
    	//根据用户和课程取消
    	$where['user_id'] = $_GET['user_id'];
    	$where['course_id'] = $_GET['course_id'];
		//$where['id']=$_POST['id'];
		$id = $game->where($where)->delete(); // 删除
		$result['code']='取消收藏成功';
		$this->ajaxReturn($result,'JSON');
    }
    /**
     * 是否收藏
     */
	public function sfCollection(){
    	$game = M("User_collection");
    	//根据用户和课程取消
    	$where['user_id'] = $_GET['user_id'];
    	$where['course_id'] = $_GET['course_id'];
		//$where['id']=$_POST['id'];
		$id = $game->where($where)->select();
		if(empty($id)){
			$result['code']='1';
			$result['msg']='未收藏';
			$this->ajaxReturn($result,'JSON');
		}else{
			$result['code']='2';
			$result['msg']='已收藏';
			$this->ajaxReturn($result,'JSON');
		}
    }
    /**
     * 提交申请
     */
    public function addApplication(){
    	$data['course_id'] = $_POST['course_id'];
    	$data['user_id'] = $_POST['user_id'];
    	$data['student_name'] = $_POST['student_name'];
    	$data['student_sex'] = $_POST['student_sex'];
    	$data['brithday'] = $_POST['brithday'];
    	$data['parent_name'] = $_POST['parent_name'];
    	$data['parent_sex'] = $_POST['parent_sex'];
    	$data['phone'] = $_POST['phone'];
    	$data['order_status'] = '已预约';
    	$user_appointment = M('User_appointment');
    	$user_appointment->create();
		$curr_time = date('Y-m-d H:i',time());
		$data['create_time'] = $curr_time;
		//$user_appointment->create_time=$curr_time;
		$id = $user_appointment->data($data)->add();
		$result['code'] = '申请成功!';
		$this->ajaxReturn($result,'JSON');
    }
    /**
     * 我的预约
     */
    public function myApplication(){
    	$user_appointment = M('User_appointment');
    	$where['s_user_appointment.user_id'] = $_GET['id'];
    	$userAppointment = $user_appointment->field("s_user_appointment.*,c.course_name,ci.image,sc.name as center_name,c.type as course_type") 
    	->join("left join s_course c on c.id = s_user_appointment.course_id 
    		left join s_center sc on c.center_id = sc.id 
        	left JOIN s_course_image ci on c.id = ci.course_id and ci.type='1'")
    	->where($where) ->order('s_user_appointment.create_time desc')-> select();
    	$result['code']='1';
    	$result['userAppointment'] = $userAppointment;
    	$this->ajaxReturn($result,'JSON');
    }
    /**
     * 我的预约详细
     */
    public function myApplicationDetail(){
    	$user_appointment = M('User_appointment');
    	$where['s_user_appointment.id'] = $_GET['id'];
    	$userAppointment = $user_appointment->field('s_user_appointment.*,c.course_name,sc.name as center_name,
    	(year(now()) - year(s_user_appointment.brithday)) as years')
    	->join('left join s_course c on c.id = s_user_appointment.course_id 
    	left join s_center sc on c.center_id = sc.id ')->where($where)->find();
    	$result['code']='1';
    	$result['userAppointment'] = $userAppointment;
    	$this->ajaxReturn($result,'JSON');
    }
    /**
     * 全部评论
     */
    public function courseName(){
    	$course_comment = M('Course');
    	$where['id'] = $_GET['id'];
    	$course_name = $course_comment->where($where)->getField('course_name');;
    	//$result['courseComment'] = $courseComment;
    	$this->ajaxReturn($course_name,'JSON');
    }
    /**
     * 计算经纬度用
     * Enter description here ...
     * @param unknown_type $user_long
     * @param unknown_type $user_lat
     * @param unknown_type $city
     */
	private  function updateUserDistance($user_long,$user_lat)
	{
		 
		$course=M('Center');
		$members=$course->field('s_center.*')->join('RIGHT JOIN s_course sc on sc.center_id = s_center.id')->select();	
		$options['distance_id']=rand(1,9999);
		$distance_id=$options['distance_id'];
		foreach ($members as $key=>$value)
		{
			$user_distance=M('User_distance');
			$temp_distance=round((float)getdistance($user_long,$user_lat,$value['long'],$value['lat'])/1000,2);
			$options['temp_distance']=$temp_distance;
			$options['center_id']=$value['id'];
			//$options['id']=UUIDUtil::getOneUUID();//主键设置为UUID(varchar类型)
			$user_distance->data($options)->add();
		}
		return $distance_id;
	}
	 /**
	*求两个已知经纬度之间的距离,单位为米
	*@param lng1,lng2 经度
	*@param lat1,lat2 纬度
	*@return float 距离，单位米
	*@author www.Alixixi.com
	**/
	/*function getdistance($lng1,$lat1,$lng2,$lat2){
		//将角度转为狐度
		$radLat1=deg2rad($lat1);//deg2rad()函数将角度转换为弧度
		$radLat2=deg2rad($lat2);
		$radLng1=deg2rad($lng1);
		$radLng2=deg2rad($lng2);
		$a=$radLat1-$radLat2;
		$b=$radLng1-$radLng2;
		$s=2*asin(sqrt(pow(sin($a/2),2)+cos($radLat1)*cos($radLat2)*pow(sin($b/2),2)))*6378.137*1000;
		return $s;
	}*/
}
?>